package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.*;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 添加一条订单数据
     * @param orders
     */
    void insert(Orders orders);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 查询历史订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 订单查询
     *
     * @param orderId
     */
    @Select("select * from orders where id = #{orderId}")
    OrderVO selectById(Long orderId);

    /**
     * 取消订单
     * @param
     */
    @Select("update orders set cancel_time = #{cancelTime},cancel_reason=#{ordersCancelDTO.cancelReason},status = #{status} where id = #{ordersCancelDTO.id}")
    void updateById(Integer status ,OrdersCancelDTO ordersCancelDTO, LocalDateTime cancelTime);
    
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 状态统计
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer statusCount(Integer status);

    /**
     * 商家接单
     * @param ordersConfirmDTO
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void confirmOrders(OrdersConfirmDTO ordersConfirmDTO);

    /**
     * 拒单
     * @param ordersRejectionDTO
     */
    @Update("update orders set rejection_reason = #{rejectionReason},status= 6 where id = #{id}")
    void rejectionOrders(OrdersRejectionDTO ordersRejectionDTO);

    /**
     * 商家取消订单
     * @param ordersCancelDTO
     */
    @Update("update orders set status = #{status},cancel_time=#{cancelTime},cancel_reason = #{ordersCancelDTO.cancelReason} where id = #{ordersCancelDTO.id}")
    void adminCancel(OrdersCancelDTO ordersCancelDTO ,Integer status ,LocalDateTime cancelTime);

    /**
     * 派送
     * @param id
     * @param status
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void delivery(Long id, Integer status);

    /**
     * 完成订单
     * @param id
     * @param status
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void complete(Long id, Integer status);

    /**
     * 查询超时订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    /**
     * 每天营业额统计
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 订单统计
     * @param map
     * @return
     */
    Integer orderCount(Map map);

    /**
     * 统计排名前十的菜品或套餐
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);

    Integer countByMap(Map map);
}
